// @ts-nocheck
import { store } from "@/store";
import { WAChatManager } from "@/chat/manager";

export const useChatStore = defineStore("chat", () => {
  const data = ref({
    manager: "",
  });

  function openSocket() {
    data.value.manager = new WAChatManager("ws://47.251.98.213:3000/whatsapp/web/chat");
  }

  function linkClient(clientId, getMessage) {
    data.value.getMessage = getMessage;
    data.value.manager.createChat(JSON.stringify({ clientId }), saveQrcode);
  }

  function saveQrcode(qrCode) {
    data.value.qrCode = qrCode;
    data.value.getMessage && data.value.getMessage(qrCode);
  }

  return {
    openSocket,
    linkClient,
    data,
  };
});

export function useChatStoreHook() {
  return useChatStore(store);
}
